package io.geekidea.boot.system.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@Slf4j
@RestController
@RequestMapping("/sysOcrPdfBox")
@Tag(name = "PDF识别解析")
@RequiredArgsConstructor
public class SysOcrPdfBox {
    @PostMapping("/pdfBox")
    public ResponseEntity<String> pdfBox(MultipartFile multipartFile, HttpServletRequest request ) throws Exception {
        try {
            // 创建临时文件
            File tempFile = File.createTempFile("temp", ".pdf");
            // 将上传的文件写入临时文件
            multipartFile.transferTo(tempFile);
            // 加载 PDF 文档
            try (FileInputStream fis = new FileInputStream(tempFile);
                 PDDocument document = PDDocument.load(fis)) {
                // 使用 PDFTextStripper 工具
                PDFTextStripper textStripper = new PDFTextStripper();
                // 设置文本排序，有规则输出
                textStripper.setSortByPosition(true);
                // 获取所有文字信息
                return ResponseEntity.ok(textStripper.getText(document));
            }
        } catch (IOException | RuntimeException e) {
            // 捕获异常并进行适当的处理
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred during OCR processing.");
        }
    }
    @PostMapping("/pdfOcr")
    public ResponseEntity<String> pdfOcr( MultipartFile multipartFile) {
        try {
            // 创建临时文件
            File tempFile = File.createTempFile("temp", getFileExtension(multipartFile));
            // 将上传的文件写入临时文件
            multipartFile.transferTo(tempFile);
            // 语言库位置
            String languagePath = "D:\\Information_technology\\workspace_study_demo\\spring-boot-plus\\springboot-plus\\src\\main\\resources\\tessdata";
            ITesseract instance = new Tesseract();
            // 设置训练库的位置
            instance.setDatapath(languagePath);
            // chi_sim：简体中文，eng：英语
            instance.setLanguage("chi_sim");
            String result = instance.doOCR(tempFile);
            return ResponseEntity.ok(result);
        } catch (IOException | TesseractException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred during OCR processing.");
        }
    }
    // 获取文件扩展名
    private String getFileExtension(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        if (originalFilename != null && originalFilename.contains(".")) {
            return originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        return "";
    }
}
